﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace ifunction
{
    /// <summary>
    /// Class CommonExtension.
    /// </summary>
    public static class CommonExtension
    {
        /// <summary>
        /// Gets the string value.
        /// </summary>
        /// <param name="anyObject">Any object.</param>
        /// <returns>System.String.</returns>
        public static string GetStringValue(this object anyObject)
        {
            return anyObject == null ? string.Empty : anyObject.ToString();
        }

        /// <summary>
        /// Checks the null object.
        /// </summary>
        /// <param name="anyObject">Any object.</param>
        /// <param name="objectName">Name of the object.</param>
        /// <exception cref="System.NullReferenceException"></exception>
        public static void CheckNullObject(this object anyObject, string objectName)
        {
            if (anyObject == null)
            {
                throw new NullReferenceException(string.Format("Object [{0}] is null or empty.", objectName));
            }
        }

        /// <summary>
        /// Gets the attribute value.
        /// </summary>
        /// <param name="xElement">The x element.</param>
        /// <param name="attribute">The attribute.</param>
        /// <returns>System.String.</returns>
        public static string GetAttributeValue(this XElement xElement, string attribute)
        {
            string result = string.Empty;

            if (xElement != null && !string.IsNullOrWhiteSpace(attribute))
            {
                var attr = xElement.Attribute(attribute);
                if (attr != null)
                {
                    result = attr.Value;
                }
            }

            return result;
        }

        /// <summary>
        /// Determines whether the specified element has elements.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="name">The name.</param>
        /// <returns><c>true</c> if the specified element has elements; otherwise, <c>false</c>.</returns>
        public static bool HasElements(this XElement element, string name = null)
        {
            return element != null
                && (!string.IsNullOrEmpty(name) ? element.Elements(name).Count() > 0 : element.Elements().Count() > 0);
        }

        /// <summary>
        /// Merges the specified instance.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="V"></typeparam>
        /// <param name="instance">The instance.</param>
        /// <param name="keyValueCollection">The key value collection.</param>
        public static void Merge<T, V>(this Dictionary<T, V> instance, ICollection<KeyValuePair<T, V>> keyValueCollection)
        {
            if (instance != null && keyValueCollection != null)
            {
                if (keyValueCollection.Count > 0)
                {
                    foreach (var one in keyValueCollection)
                    {
                        T key = one.Key;
                        V value = one.Value;

                        if (key != null)
                        {
                            instance.Merge(key, value);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Merges the specified instance.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="V"></typeparam>
        /// <param name="instance">The instance.</param>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        public static void Merge<T, V>(this Dictionary<T, V> instance, T key, V value)
        {
            if (instance != null && key != null)
            {
                if (instance.ContainsKey(key))
                {
                    instance[key] = value;
                }
                else
                {
                    instance.Add(key, value);
                }
            }
        }
    }
}
